<!--
* description: 备件交期管理-BO交期人工答复配置
* author: zouzhu
* createdDate: 2021-01-28
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-table-cols="tableCols"
      :dynamic-api-config="apiConfig"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-show-more-btn="true"
      :dynamic-is-init-table="true"
      :callback-obj="callbackObj"
    />
  </div>
</template>

<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import api from '@/api/pa/sparepartsDeliveryManagement'
import Vue from 'vue'

export default {
  name: 'ManualReplySettingBO',

  components: {
    OneTableTemplate
  },

  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      apiConfig: api.rewardTypeMaintenanceQuery,

      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          name: 'confirm',
          clickEvent: () => this.save(),
          text: '保存',
          position: 'left',
          disabled: true
        },
        {
          compKey: 'btnKey3',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: '重置',
          position: 'right'
        }
      ],

      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '订单类型：',
              codeField: 'lookupValueCode',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'lookupValueCode,lookupValueName',
              customObj: {
                api: api.queryDropDownList,
                selectTye: '1'
              },
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '是否答复：',
              codeField: 'isManualReply',
              textField: 'isManualReplyCn',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'isManualReply,isManualReplyCn',
              customObj: {
                api: api.isManualReply,
                selectTye: '1'
              },
              type: 'dropdownList',
              isMul: false,
              isMust: true
            }
          ],

      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'ordertypeBoId',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'ordertypeId',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制字段',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'lookupValueName',
              label: '订单类型'
            },
            {
              prop: 'isManualReply',
              label: '人工答复',
              isComponent: true,
              isColComp: true,
              comps: [
                {
                  isAsync: true,
                  clearable: false,
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'isManualReply',
                  textField: 'isManualReplyCn',
                  component: () => import('@/components/org/commonSelect'),
                  oFields: 'isManualReply,isManualReplyCn',
                  customObj: {
                    api: api.isManualReply,
                    selectTye: '1'
                  },
                  type: 'dropdownList',
                  isMul: false
                }
              ]
            },
            {
              prop: 'remark',
              label: '备注',
              isComponent: true,
              comps: [
                {
                  compKey: 'compKey5' + this.$utils.generateId(),
                  codeField: 'remark',
                  component: () => import('@/components/org/commonInput'),
                  type: 'inputText'
                }
              ]
            }
          ],

      formField: this.$utils.getFormField(this, {
        lookupValueCode: '',
        isManualReply: '',
        ordertypeId: '',
        ordertypeBoId: '',
        updateControlId: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode]),

      callbackObj: {
        // 当选择项发生变化时会触发该事件（多选时）
        onCheckSelectChange: (selection) => {
          this.selectData = selection

          this.handleSelectionChange(selection)
        }
      }
    }
  },

  mounted() {
    this.queryTable(1)
  },

  methods: {
    // 保存
    save() {
      // debugger
      this.$confirm('确定要保存吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const queryObj = {
        // 保存需要传 type="mutation"
          type: 'mutation',
          apiConfig: api.saveDelivery,
          // 条件/实体参数（变量），根据typeParam中的定义配置

          variables: {
            dataInfo: this.selectData
          }
        }
        // 转换了中台请求格式数据
        var paramD = this.$getParams(queryObj)
        this.$requestAPI(paramD).then((response) => {
          if (response.result === '1') {
            this.$message({
              message: response.msg
            })
          } else {
            this.$message({
              message: response.msg
            })
          }
          this.queryTable(1)
        })
      })
    },

    // 当选择项发生变化时会触发该事件
    handleSelectionChange(selection) {
      const btns = this.tableButtons.filter(o => o.disabled !== undefined)
      if (btns.length > 0) {
        const isCheck = selection && selection.length > 0
        btns.forEach((item) => {
          item.disabled = !isCheck
        })
      }
    }
  }
}
</script>
